<?php
/**
 * --------------------------------------------------------------
 * 手册文档控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Manual as ManualModel;
use app\admin\model\ManualDoc as ManualDocModel;

class ManualDoc extends AdminBase
{
    /**
     * 新增
     */
    public function save()
    {
        $manual_id = $this->request->post('manual_id');
        if ($this->request->isPost()) {
            $document      = null;
            $is_set_inc    = false;
            $content       = $this->request->post('editormd-markdown-doc', null);
            
            if(empty($content) === false) {
                $id        = intval($this->request->post('id'));
                if ($id <= 0) return $this->error('参数错误');
                // 保存修改文档内容
                $document = ManualDocModel::where('id', $id)->field('id,pid,doc_name,doc_content')->find();
                if (empty($document)) return $this->error('没有找到该文档');
                if (strcasecmp(md5($content), md5($document->doc_content)) === 0) {
                    $data = [
                        'id'   => $id,
                        'pid'  => $document->pid,
                        'name' => $document->doc_name
                    ];
                    return ['code' => 1, 'msg' => '文档保存成功', 'data' => $data];
                }
                $document->doc_content = $content;
                $document->id          = $id;
            } else {
                // 新建文档
                $id   = intval($this->request->post('id'));
                $pid  = intval($this->request->post('pid'));
                $name = trim($this->request->post('documentName', ''));
                $sort = intval($this->request->post('sort'));
                
                if (empty($name)) return $this->error('文档名称不能为空');
                
                //查看是否存在指定的文档
                if ($id > 0) {
                    $document = ManualDocModel::where(['manual_id' => $manual_id, 'id' => $id])->field('id,doc_name')->find();
                    if (empty($document)) {
                        return $this->error('参数错误');
                    }
                }
                
                // 判断父文档是否存在
                if ($pid > 0) {
                    $select_pid = ManualDocModel::where(['manual_id' => $manual_id, 'id' => $pid])->value('id');
                    if (empty($select_pid)) return $this->error('父文档不存在');
                }
                
                // 判断同一个项目中是否存在重名文档
                $doc_id = ManualDocModel::where(['manual_id' => $manual_id, 'doc_name' => $name])->value('id');
                if (empty($doc_id) === false) return $this->error('该项目中存在重名文档');
                
                if (empty($select_doc) === false && $select_doc->pid == $pid && strcasecmp($select_doc->doc_name, $name) === 0 && $sort <= 0) {
                    return ['code' => 1, 'msg' => '文档保存成功', 'data' => ['id' => $id, 'pid' => $pid, 'name' => $name]];
                }
                
                $document = $document ?: new ManualDocModel();
                
                $document->manual_id = $manual_id;
                $document->doc_name  = $name;
                $document->pid       = $pid;
                
                if ($id <= 0) {
                    $sort = ManualDocModel::where(['pid' => $pid])->order('sort desc')->value('sort');
                    $sort = ($sort ? $sort : -1) + 1;
                }
                
                if ($sort > 0) $document->sort = $sort;
                if ($id == 0) $is_set_inc = true;
            }
            
            if ($document->save() === false) return $this->error('文档保存失败');
            if ($is_set_inc) ManualModel::where('id', $manual_id)->setInc('doc_count');
            
            $data = [
                'id'   => $document->id,
                'pid'  => ($document->pid == 0 ? '#' : $document->pid.''),
                'name' => $document->doc_name
            ];
            return ['code' => 1, 'msg' => '文档保存成功', 'data' => $data];
        }
        
        return $this->error('错误的请求方式');
    }
    
    /**
     * 编辑读取
     */
    public function edit($id)
    {
        if (empty($id) || $id <= 0) return $this->error('参数错误');
        
        if ($this->request->isPost()) {
        } else {
            $doc = ManualDocModel::get($id);
            
            if (empty($doc)) return $this->error('文档读取错误');
            
            $data = [
                'doc' => [
                    'id'        => $doc->id,
                    'name'      => $doc->doc_name,
                    'manual_id' => $doc->manual_id,
                    'pid'       => $doc->pid,
                    'content'   => $doc->doc_content,
                ]
            ];
            
            return ['code' => 1, 'data' => $data, 'msg' => '文档加载成功'];
        }
    }
    
    /**
     * 删除文档
     */
    public function delete($id)
    {
        $id = intval($id);
        if($id <= 0){
            return $this->error('参数错误');
        }

        $doc = ManualDocModel::where('id', $id)->value('id,manual_id');
        //如果文档不存在
        if(empty($doc)){
            return $this->error('该文档不存在');
        }
        $ManualDocModel = new ManualDocModel();
        $result         = $ManualDocModel->deleteDocument($id);
        $dec_num        = count($result);
        ManualModel::where('id', $doc['manual_id'])->setDec('doc_content', $dec_num);

        return $result ? $this->success('删除成功') : $this->error('删除失败');
    }
    
    /**
     * 拖动排序
     */
    public function sort($manual_id)
    {
        if (empty($manual_id) || $manual_id <= 0) return $this->error('参数错误'); 
        $params = $this->request->post('sortData');
        if(empty($params) === false){
            $params = json_decode($params, true);

            if(empty($params) === false){
                foreach ($params as $item){
                    $data = ['pid' => $item['parent'], 'sort' => $item['sort']];

                    ManualDocModel::where(['manual_id' => $manual_id, 'id' => $item['id']])->update($data);
                }
            }
        }
        return $this->success('保存排序成功');
    }
}